package org.example.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.example.common.base.constants.AuthConstant;
import org.example.common.base.domain.UserDto;
import org.example.common.base.util.web.AjaxResult;
import org.example.system.dao.SysRoleDao;
import org.example.system.dao.SysUserDao;
import org.example.system.dao.SysUserRoleDao;
import org.example.system.entity.SysRole;
import org.example.system.entity.SysUser;
import org.example.system.service.IAuthService;
import org.example.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统用户 服务实现类
 * </p>
 *
 * @author LiJunYi
 * @since 2020-05-25
 */
@Service
@Slf4j
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUser> implements ISysUserService {

    @Autowired
    SysUserDao userDao;
    @Autowired
    SysRoleDao roleDao;
    @Autowired
    SysUserRoleDao userRoleDao;
    @Autowired
    private IAuthService authService;

    /**
     * 获取通过用户名
     *
     * @param username 用户名
     * @return {@link SysUser}
     */
    @Override
    public SysUser getByUsername(String username)
    {
       return userDao.getByUsername(username);
    }

    @Override
    public List<SysUser> getList()
    {
        return userDao.getList();
    }

    /**
     * 登录
     *
     * @param username 用户名
     * @param password 密码
     * @return {@link AjaxResult}
     */
    @Override
    public AjaxResult login(String username, String password) {
        if(StrUtil.isEmpty(username)||StrUtil.isEmpty(password)){
           return AjaxResult.error("用户名或密码不能为空！");
        }
        Map<String, String> params = new HashMap<>();
        params.put("client_id", AuthConstant.ADMIN_CLIENT_ID);
        params.put("client_secret","123456");
        params.put("grant_type","password");
        params.put("username",username);
        params.put("password",password);
        AjaxResult result = authService.getAccessToken(params);
        int code = Convert.toInt(result.get(AjaxResult.CODE_TAG));
        if (code == 200 && ObjectUtil.isNotNull(result.get(AjaxResult.DATA_TAG))){
            // 登录成功
            log.info("login success");
        }
        return result;
    }

    @Override
    public UserDto loadUserByUsername(String username) {
        SysUser sysUser = userDao.getByUsername(username);
        if (ObjectUtil.isNull(sysUser))
        {
            return null;
        }
        // 获取当前用户的角色信息
        List<SysRole> userRoles = roleDao.getRolesByUserId(sysUser.getId());
        UserDto userDTO = new UserDto();
        if (CollUtil.isNotEmpty(userRoles)){
            List<String> roleStrList = userRoles.stream().map(item -> item.getId() + "_" + item.getName()).collect(Collectors.toList());
            userDTO.setRoles(roleStrList);
        }
        userDTO.setUsername(username);
        userDTO.setId(sysUser.getId());
        userDTO.setStatus(1);
        userDTO.setPassword(sysUser.getPassword());
        userDTO.setClientId(AuthConstant.ADMIN_CLIENT_ID);
        return userDTO;
    }
}
